//
// Created by 吴喆 on 2020/3/21.
//

#include "BackgroundRenderer.h"

//
// Created by 吴喆 on 2020/3/5.
//
BackgroundRenderer::BackgroundRenderer(Shader *shader) : Renderer(shader) {
    initRenderData();
}

void BackgroundRenderer::initRenderData() {
    // Configure VAO/VBO
    GLuint VBO;
    GLfloat vertices[] = {
            // Pos      // Tex
            -1.0f, 1.0f, 0.0f, 1.0f,
            -1.0f, -1.0f, 0.0f, 0.0f,
            1.0f, -1.0f, 1.0f, 0.0f,

            -1.0f, 1.0f, 0.0f, 1.0f,
            1.0f, -1.0f, 1.0f, 0.0f,
            1.0f, 1.0f, 1.0f, 1.0f
    };

    glGenVertexArrays(1, &this->quadVAO);
    glGenBuffers(1, &VBO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindVertexArray(this->quadVAO);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid *) 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}


void BackgroundRenderer::DrawSprite(Camera *camera, Texture2D *texture, glm::vec2 position, glm::vec2 size, GLfloat rotate, bool hFlip, bool vFlip, bool useColor, glm::vec4 color) {

    Renderer::shader->use();
    this->shader->setInteger("needFlipH", hFlip);
    this->shader->setInteger("needFlipV", vFlip);
    this->shader->setInteger("image", 0);
    this->shader->setMatrix4("projection", glm::mat4());
    this->shader->setMatrix4("view", glm::mat4());
    this->shader->setMatrix4("model", glm::mat4());
    // Render textured quad
    this->shader->setVector3f("spriteColor", color);
    this->shader->setInteger("exist", 0);

    glActiveTexture(GL_TEXTURE0);
    texture->bind();
    glBindVertexArray(this->quadVAO);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);
    shader->disuse();
}



